\documentclass[a4paper]{article}

\usepackage[intlimits]{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{ifpdf}

\ifpdf
\else
\def\pgfsysdriver{pgfsys-dvipdfm.def}
\fi
\usepackage{tikz}
\usepackage{pgfplots}

\parindent=0pt

\def\testsection#1{\message{---------- STARTING TEST SECTION '#1'}\section{#1}}
\def\testsubsection#1{\message{-------STARTING TEST SUBSECTION '#1'}\subsection{#1}}
\def\testsubsubsection#1{\message{------STARTING TEST SUBSUBSECTION '#1'}\subsubsection{#1}}

\author{Christian Feuers\"anger}
\title{Test cases for pgfkeysaddition.sty}

\begin{document}
\maketitle

\pgfkeys{/my group/option1/.code={\message{/my group/option1 set to '#1'!}}}
\pgfkeys{/my group/option2/.code={\message{/my group/option2 set to '#1'!}}}
\pgfkeys{/my other group/O1/.code={\message{/my other group/O1 set to '#1'!}}}
\pgfkeys{/my other group/O2/.code={\message{/my other group/O2 set to '#1'!}}}

\pgfkeys{/pgf/key filter handlers/log/.install key filter handler}%

\testsection{Testing the `is descendant of -- predicate'}
\pgfkeys{/pgf/key filters/is descendant of/.install key filter={/my group}}%
\tracingmacros=2\tracingcommands=2
\pgfkeysfiltered%
	{/my group/.cd,option1=value for option1,/my other group/O1=value for O1}%
\tracingmacros=0\tracingcommands=0

\testsubsection{Testing it for a tikz style}
\tikzstyle{mein style}=[/my group/option1=option1 value in a style!,/tikz/line width=1pt]
\pgfkeys{/pgf/key filters/is descendant of/.install key filter={/my group}}%
\pgfkeysfiltered%
	{/my group/.cd,option1=value for option1,/my other group/O1=value for O1,/tikz/mein style}%


\testsection{Testing filtering for errors}
\testsubsection{unknown handler in /my group/}
{
	\pgfkeys{/my group/.unknown/.code=\message{'#1' is unknown and the handler /my group/.unknown has been invoked!}}%
	\pgfkeys{/pgf/key filters/is descendant of/.install key filter={/my group}}%
	\pgfkeysfiltered%
		{/my group/.cd,option1=value for option1,option42=42}%
}

\testsubsection{unknown handler in /handlers/}
{
	\pgfkeys{/handlers/.unknown/.code=\message{'#1' is unknown and the handler /handlers/.unknown has been invoked!}}%
	\pgfkeys{/pgf/key filters/is descendant of/.install key filter={/my group}}%
	\pgfkeysfiltered%
		{/my group/.cd,option1=value for option1,option42=42}%
}


\testsubsection{Check whether error messages come through}
{
	\pgfkeys{/errors/mein fehler/.code=\message{My error message came through. That's good!}}%
	\pgfkeys{/handlers/.unknown/.code=\pgfkeysalso{/errors/mein fehler=foo}}%
	\pgfkeys{/pgf/key filters/is descendant of/.install key filter={/my group}}%
	\pgfkeysfiltered%
		{/my group/.cd,option1=value for option1,option42=42}%
}

\testsection{Checking single key selection}
{
	\pgfkeys{/test/a/.code=\message{'/test/a' is currently running; that's correct.}}%
	\pgfkeys{/test/b/.code=\errmessage{'/test/b' shouldn't run! test failed!}}%
	\pgfkeyssetvalue{/test/c}{Value of 'c'}%
	\pgfkeys{/test/d/.style={/test/a=bar}}%

	\testsubsection{command keys: select only /test/a}
	\pgfkeys{/pgf/key filters/equals/.install key filter={/test/a}}
	\pgfkeysfiltered%
		{/test/a=4,/test/b=5}

	\testsubsection{value keys: select only /test/c}
	\pgfkeys{/pgf/key filters/equals/.install key filter={/test/c}}
	\pgfkeysfiltered%
		{/test/a=4,/test/b=5,/test/c=7}

	\testsubsection{handler keys: select only /test/d}
	\pgfkeys{/pgf/key filters/equals/.install key filter={/test/d}}
	\pgfkeysfiltered%
		{/test/a=4,/test/b=5,/test/d}
}


\testsection{Checking logical combinations}
{
	\pgfkeys{/test/a/.code=\message{'/test/a' is currently running; that's correct.}}%
	\pgfkeys{/test/b/.code=\errmessage{'/test/b' shouldn't run! test failed!}}%
	\pgfkeys{/test/c/.code=\message{'/test/c' is currently running; that's correct.}}%
	\pgfkeys{/my group/A/.code=\message{'/my group/A' is currently running, that's correct.}}

	\testsubsection{NOT = /test/b}
	\pgfkeys{/pgf/key filters/not/.install key filter={/pgf/key filters/equals=/test/b}}
	\pgfkeysfiltered%
		{/test/a=4,/test/b=5,/test/c}

	\testsubsection{AND}
	\pgfkeys{/pgf/key filters/and/.install key filter={{/pgf/key filters/equals={/test/a}}{/pgf/key filters/is descendant of=/test}}}
	\pgfkeysfiltered%
		{/test/a=4,/test/b=5,/test/c}

	\testsubsection{OR}
	\pgfkeys{/pgf/key filters/or/.install key filter={{/pgf/key filters/equals={/test/a}}{/pgf/key filters/is descendant of=/my group}}}
	\pgfkeysfiltered%
		{/test/a=4,/test/b=5,/my group/A=value 1}
}

\testsection{Testing filtered key collection}
{
	\testsubsection{full keys}
	\tracingmacros=2\tracingcommands=2
	\pgfkeys{/pgf/key filters/is descendant of/.install key filter=/my group}
	\def\filtered{}%
	\pgfkeys{/pgf/key filter handlers/append filtered to/.install key filter handler=\filtered}%
	\pgfkeysfiltered%
		{/my group/.cd,option1=value for option1,/my other group/O1=value for O1,/my other group/O2=value for O2}%
	\message{filtered = '\filtered'}
	\filtered

	\testsubsection{partial keys}
	\def\filtered{}%
	\pgfkeys{/pgf/key filters/or/.install key filter={{/pgf/key filters/is descendant of=/my group}{/pgf/key filters/equals=/my other group/.cd}}}
	\pgfkeysfiltered%
		{/my group/.cd,option1=value for option1,/my other group/.cd,O1=value for O1,O2= value for O2}%
	\tracingmacros=2\tracingcommands=2
	\message{filtered = '\filtered'}
	\tracingmacros=0\tracingcommands=0
	\filtered

	\testsubsubsection{setting remaining keys}
	\pgfkeys{/my other group/.cd,/utils/exec=\pgfkeysalsofrom\filtered}
}%


\iffalse
\testsection{Testing search paths}
{
	\pgfkeys{/my search path/.unknown/.code={%
			\let\searchname=\pgfkeyscurrentname%
			\pgfkeysalso{%
				/my group/\searchname/.try=#1,
				/my other group/\searchname/.lastretry=#1
			}%
		}%
	}
	\pgfkeys{/my other group/.unknown/.code=\message{/my other/group/.unknown: called with unknown option '#1'.}}%

	\testsubsection{First: no filtering}
	\pgfkeys{/my search path/.cd,option1=value for option1,O1=value for O1,an unknown option=foo}

	%--------------------------------------------------
	% \testsubsection{Second: with filtering}
	% \pgfkeysfiltered%
	% 	{\pgfkeysisdescendantoftwo{/my group}{/my search path}}%
	% 	{\pgfkeyslogfiltered}%
	% 	{/my search path/.cd,option1=value for option1,O1=value for O1,an unknown option=foo}
	% }
	%
	%--------------------------------------------------
}
\fi

\testsection{Testing pgfkeysalsofiltered}
{
	\def\filtered{}%
	\pgfkeys{/pgf/key filter handlers/append filtered to/.install key filter handler=\filtered}%

	\pgfkeys{/pgf/key filters/is descendant of/.install key filter=/my group}
	\pgfkeys{/my group/.cd,/utils/exec=\pgfkeysalsofiltered
			{option1= value for option1,/my other group/O1=value for O1}}
}

\testsection{Testing family handling}
{
	\testsubsection{testing .is family init:}
	\pgfkeys{/my group/.is family}
	\pgfkeysisfamilyactive{/my group}
	\ifpgfkeysfiltercontinue
		\errmessage{TEST FAILED: /my group active: TRUE (EXPECTED FALSE)}%
	\else
		\message{/my group active: FALSE (correct)}%
	\fi

	\testsubsection{testing .activate family:}
	\pgfkeys{/my group/.activate family}
	\pgfkeysisfamilyactive{/my group}
	\ifpgfkeysfiltercontinue
		\message{/my group active: TRUE (correct)}%
	\else
		\errmessage{TEST FAILED: /my group active: FALSE (EXPECTED: TRUE)}%
	\fi

	\testsubsection{testing .deactivate family:}
	\pgfkeys{/my group/.deactivate family}
	\pgfkeysisfamilyactive{/my group}
	\ifpgfkeysfiltercontinue
		\errmessage{TEST FAILED: /my group active: TRUE (EXPECTED FALSE)}%
	\else
		\message{/my group active: FALSE (correct)}%
	\fi

	\testsubsection{Testing filtering by active family}
	{
		\pgfkeys{/family1/.is family}
		\pgfkeys{/family2/.is family}
		\pgfkeys{/test/a/.code=\message{'/test/a' is currently running; that's correct.}}%
		\pgfkeys{/test/a/.belongs to family=/family1}

		\pgfkeys{/test/b/.code=\errmessage{'/test/b' shouldn't run! test failed!}}%
		\pgfkeys{/test/b/.belongs to family=/family2}%

		\pgfkeys{/test/c/.code=\errmessage{'/test/c' shouldn't run! test failed! }}%
		\pgfkeys{/test/d/.code=\message{'/test/d' currently running. Ok.}}
		\pgfkeys{/test/d/.belongs to family=/family2}

		\testsubsubsection{first: only /test/a should run}%
		\pgfkeys{/family1/.activate family}
		\pgfkeys{
			/pgf/key filters/active families/.install key filter,
			/pgf/key filter handlers/log/.install key filter handler}

		\pgfkeysfiltered
			{/test/a=value for a,/test/b=value for b,/test/c=value for c}

		\testsubsubsection{second: no option should run}%
		\pgfkeys{/family1/.deactivate family}

		\pgfkeysfiltered
			{/test/a=value for a,/test/b=value for b,/test/c=value for c}


		\testsubsubsection{once more again with dynamic family list detection pgfqkeysactivatefamiliesandfilteroptions}
		\tracingmacros=2\tracingcommands=2
		\pgfqkeysactivatefamiliesandfilteroptions{/family1,/family2}{/test}
			{a=value for a,c=value for c,d=value for d}
		\tracingmacros=0\tracingcommands=0
	}
}


\end{document}
